import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/base_unit_stop_list_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

/// 装置开停车大检修台账（列表）
class EquipmentStartShutdownController extends GetxController {
  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索条件
  String? searchX;

  String? stateNameBack = '请选择';
  String? stateKind = '';

  String? stateApproveNameBack = '请选择';
  String? stateApproveKind = '';
  /// 状态 0停产 1在产 2检维修
  List<GetDictAllEntity> baseUnitStopListState = [];
  /// 审批状态
   List<GetDictAllEntity> baseUnitStopListApproveState = [];
  @override
  void onInit() {
    super.onInit();
    //调研类型 状态 0停产 1在产 2检维修
    initBaseUnitStopListState();
    //审批状态 0：待发起 1：待审批 2：审批中 3：通过 4：驳回
    initBaseUnitStopListApproveState();
  }

  /// 获取数据 http://117.173.150.167:32000/currency//baseUnitStop/list?status=0&checkType=0&deviceName=213&pageSize=15&pageNum=1
  Future<List<BaseUnitStopListEntity>> baseUnitStopList(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<BaseUnitStopListEntity>>(
      Interface.baseUnitStopList,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'deviceName': searchX, //关键字
        'status': stateKind, //状态 0停产 1在产 2检维修
        'checkType': stateApproveKind, //审批状态 0：待发起 1：待审批 2：审批中 3：通过 4：驳回
        'isApprovalPage': 1, //是否查询审批数据 1是 0否
      },
    ).execute();
    List<BaseUnitStopListEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  ///筛选按钮
  void onFilterButtonPressed() {
    riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 重置
  void reset() {
    stateKind = '';
    stateNameBack = '请选择';
    stateApproveKind = '';
    stateApproveNameBack = '请选择';
    globalKey.currentState!.onRefresh();
    update();
  }

  /// 确定
  void confirm() {
    Get.back();
    globalKey.currentState!.onRefresh();
    update();
  }

  GetDictAllEntity? dictEntity2;

  /// 状态
  void selectHiddenDangerInvestigationTaskWorkKindType() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '状态',
      dataList: baseUnitStopListState,
      checkedList: [dictEntity2 ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity2 = value[0];
        stateKind = value[0].getId();
        stateNameBack = value[0].getShowName().isEmpty ? '请选择' : value[0].getShowName();
        update();
      },
    );
  }

  GetDictAllEntity? dictEntity1;

  /// 审批状态
  void selectBaseUnitStopListApproveStateType() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '审批状态',
      dataList: baseUnitStopListApproveState,
      checkedList: [dictEntity1 ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity1 = value[0];
        stateApproveKind = value[0].getId();
        stateApproveNameBack = value[0].getShowName().isEmpty ? '请选择' : value[0].getShowName();
        update();
      },
    );
  }

  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  ///进入详情
  void onClickButtonPressed(BaseUnitStopListEntity item) {
    Get.toNamed(AppRoutes.equipmentStartShutdownApprovePage, arguments: {'id': item.id, 'isMeTask': true});
  }

  void initBaseUnitStopListState() {
    baseUnitStopListState.clear();
    final GetDictAllEntity getDictAllEntity4 = GetDictAllEntity();
    getDictAllEntity4.id = '0';
    getDictAllEntity4.dictionaryLabel = '停产';
    baseUnitStopListState.add(getDictAllEntity4);

    final GetDictAllEntity getDictAllEntity5 = GetDictAllEntity();
    getDictAllEntity5.id = '1';
    getDictAllEntity5.dictionaryLabel = '在产';
    baseUnitStopListState.add(getDictAllEntity5);

    final GetDictAllEntity getDictAllEntity6 = GetDictAllEntity();
    getDictAllEntity6.id = '2';
    getDictAllEntity6.dictionaryLabel = '检维修';
    baseUnitStopListState.add(getDictAllEntity6);
  }

  void initBaseUnitStopListApproveState() {
    baseUnitStopListApproveState.clear();
    final GetDictAllEntity getDictAllEntity7 = GetDictAllEntity();
    getDictAllEntity7.id = '0';
    getDictAllEntity7.dictionaryLabel = '待发起';
    baseUnitStopListApproveState.add(getDictAllEntity7);

    final GetDictAllEntity getDictAllEntity8 = GetDictAllEntity();
    getDictAllEntity8.id = '1';
    getDictAllEntity8.dictionaryLabel = '待审批';
    baseUnitStopListApproveState.add(getDictAllEntity8);

    final GetDictAllEntity getDictAllEntity9 = GetDictAllEntity();
    getDictAllEntity9.id = '2';
    getDictAllEntity9.dictionaryLabel = '审批中';
    baseUnitStopListApproveState.add(getDictAllEntity9);

    final GetDictAllEntity getDictAllEntity10 = GetDictAllEntity();
    getDictAllEntity10.id = '3';
    getDictAllEntity10.dictionaryLabel = '通过';
    baseUnitStopListApproveState.add(getDictAllEntity10);

    final GetDictAllEntity getDictAllEntity11 = GetDictAllEntity();
    getDictAllEntity11.id = '4';
    getDictAllEntity11.dictionaryLabel = '驳回';
    baseUnitStopListApproveState.add(getDictAllEntity11);
  }
}
